package com.yikaiye.android.yikaiye.ui.activity.login;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivitySetPasswordBinding;
import com.yikaiye.android.yikaiye.ui.activity.login.viewmodel.SetPasswordViewModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

/**
 * 重置密码
 */
public class SetPasswordActivity extends BaseActivity<ActivitySetPasswordBinding, SetPasswordViewModel> {

    private String phone;
    private String code;
    private boolean isShowPwd;
    private boolean isShowPwdAgain;

    @Override
    protected SetPasswordViewModel bindModel() {
        return getViewModel(this, SetPasswordViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        if (getIntent().getExtras() != null) {
            phone = getIntent().getExtras().getString("phone");
            code = getIntent().getExtras().getString("code");
        }
        binding.setSetPasswordViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        viewModel.password.observe(this, s -> changeBtnStatus());
        viewModel.againPassword.observe(this, s -> changeBtnStatus());
        viewModel.onDelayClick(binding.back, o -> finish());
        viewModel.onDelayClick(binding.btnFinish, o -> {
            if (!CommonUtil.checkPassword(viewModel.password.getValue())) {
                ToastHelper.show(getString(R.string.input_password));
                return;
            }
            if (!TextUtils.equals(viewModel.password.getValue(), viewModel.againPassword.getValue())) {
                ToastHelper.show(getString(R.string.password_different));
                return;
            }
            showLoadingDialog();
            viewModel.resetPassword(phone, code);
        });
        viewModel.onDelayClick(binding.ivPwdStatus, o -> {
            isShowPwd = !isShowPwd;
            binding.etPassword.setTransformationMethod(isShowPwd ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            binding.ivPwdStatus.setImageResource(isShowPwd ? R.mipmap.icon_pass_open : R.mipmap.icon_pass_close);
            binding.etPassword.setSelection(binding.etPassword.getText().toString().length());
        });
        viewModel.onDelayClick(binding.ivPwdStatusAgain, o -> {
            isShowPwdAgain = !isShowPwdAgain;
            binding.etPasswordAgain.setTransformationMethod(isShowPwdAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            binding.ivPwdStatusAgain.setImageResource(isShowPwdAgain ? R.mipmap.icon_pass_open : R.mipmap.icon_pass_close);
            binding.etPasswordAgain.setSelection(binding.etPasswordAgain.getText().toString().length());
        });
        // 重置密码成功
        viewModel.resetPasswordSuccess.observe(this, userModel -> {
            dismissLoadingDialog();
            MyApplication.getInstance().popAllActivityExceptOne();
            JumpIntent.jump(this, LoginActivity.class);
        });
        // 重置密码失败
        viewModel.resetPasswordError.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(s);
        });
    }

    private void changeBtnStatus() {
        if (!TextUtils.isEmpty(viewModel.password.getValue()) && !TextUtils.isEmpty(viewModel.againPassword.getValue())) {
            binding.btnFinish.setEnabled(true);
            binding.btnFinish.setBackgroundResource(R.drawable.shape_f93f50_4dp);
        } else {
            binding.btnFinish.setEnabled(false);
            binding.btnFinish.setBackgroundResource(R.drawable.shape_fcebea_4dp);
        }
    }
}